<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">
            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
            <script type="text/html" id="currentTableBar">
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
                <a class="layui-btn  layui-btn-xs" lay-event="read">预览</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            </script>

        </div>
    </div>
    <script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
    <script>
        layui.use(['form', 'table', 'laydate', 'cookie'], function () {
            var laydate = layui.laydate;

            //执行一个laydate实例
            laydate.render({
                elem: '#date',
            });
            laydate.render({
                elem: '#date-end',
            });
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                cookie = layui.cookie;
            //超时跳转
            $.ajaxSetup({
                complete: function (XMLHttpRequest, textStatus) {
                    if (textStatus == "parsererror") {
                        //这里不能用window.location,href，因为用了之后你会发现他是在子窗口跳转到登录页，而不是整个系统跳转
                        top.location.href = '../../views/login.html';
                    }
                }
            })
            // 初始化表格
            var tableIns = table.render({
                elem: '#currentTableId',
                url: '/news',
                where: {
                    is_pass:2
                },
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                cols: [
                    [
                        {
                            field: 'title',
                            minwidth: 150,
                            title: '新闻标题',
                        },
                        {
                            field: 'news_img',
                            width: 100,
                            title: '封面图片',
                            align: "center",
                            templet: function (d) {
                                if (d.has_img === 0) {
                                    return "无"
                                }
                                return '<p hrf="' + d.news_img +
                                    '" id="title" title="查看封面图片" onclick="showBigImage(this)"><img src="' +
                                    d.news_img + '" height="20px"></p>'
                            }
                        },
                        {
                            field: 'author',
                            width: 100,
                            title: '作者',
                            align: "center",
                            sort: true
                        },
                        {
                            field: 'date',
                            width: 160,
                            title: '发布时间',
                            align: "center",
                            sort: true,
                        },
                        {
                            width: 120,
                            title: '轮播图',
                            align: "center",
                            // 加载开关状态
                            templet: function (d) {
                                if (d.has_img === 0) {
                                    return '<input type="checkbox" disabled lay-skin="switch"  lay-text="已使用|未使用" >';
                                } else {
                                    if (d.is_using === 0) {
                                        // form.render();
                                        return '<p hasimg="' + d.has_img + '" newid="' + d.id +
                                            '" onclick="switchChange(this)"><input type="checkbox" id="switch" name="open" lay-skin="switch" lay-filter="switchTest" lay-text="已使用|未使用" ></p>';
                                    } else {
                                        // form.render();
                                        return '<p hasimg="' + d.has_img + '" newid="' + d.id +
                                            '" onclick="switchChange(this)"><input type="checkbox" id="switch" name="open" lay-skin="switch" checked lay-filter="switchTest" lay-text="已使用|未使用"></p>';
                                    }
                                }

                            }
                        },
                        {
                            width:100,
                            title:"发布人",
                            align: "center",
                            field:"staff_name"
                        },
                        {
                            width:100,
                            title:"失败原因",
                            align: "center",
                            field:"tip"
                        },
                        {
                            title: '操作',
                            width: 250,
                            toolbar: '#currentTableBar',
                            align: "center",
                            fixed: "right"
                        }
                    ]
                ],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 10,
                page: true,
                // skin: 'line'
            });
            $(function() {
                // 格式化当前时间
                function completeDate(value) {
                    return value < 10 ? "0" + value : value;
                }
                var char = "-";
                var nowDate = new Date();
                var day = nowDate.getDate() + 1;
                var month = nowDate.getMonth() + 1; //注意月份需要+1
                var year = nowDate.getFullYear();
                //补全0，并拼接
                var nowdate = year + char + completeDate(month) + char + completeDate(day);
                $("#date-end").val(nowdate);
            })
            // 放大
            showBigImage = function (e) {
                form.render();
                layer.open({
                    type: 1,
                    title: false,
                    closeBtn: 1,
                    resize: false,
                    shadeClose: true, //点击阴影关闭
                    area: '600px', //宽高
                    offset: '20%',
                    content: "<img src=" + $(e).attr('hrf') + " width='100%' height='100%'/>"
                });
            }
            // 轮播图开关
            switchChange = function (e) {

                var id = $(e).attr('newid');
                var that = $(e).children()[0];
                var is_using = (that.checked) ? is_using = 1 : is_using = 0;
                var str = {
                    "id": id,
                    "is_using": is_using
                };
                str = JSON.stringify(str);
                $.ajax({
                    url: '/news',
                    method: "PUT",
                    data: str,
                    contentType: "application/json",
                    success: function (data) {
                        layer.msg("修改成功！", {
                            icon: 1
                        });
                        // tableIns.reload();
                    },
                    error: function (err) {
                        layer.msg("服务器内部错误", {
                            icon: 2
                        });
                    }
                });


            }
            // 监听搜索操作
            // form.on('submit(data-search-btn)', function (data) {
            //     var result = JSON.stringify(data.field);
            //     // 执行搜索重载
            //     table.reload('currentTableId', {
            //         page: {
            //             curr: 1
            //         },
            //         where: {
            //             searchParams: result
            //         }
            //     }, 'data');

            //     return false;
            // });
            /**
             * toolbar监听事件
             */
            // table.on('toolbar(currentTableFilter)', function (obj) {

            //     if (obj.event === 'add') { // 监听添加操作

            //         var index = layer.open({
            //             title: '添加新闻',
            //             type: 2,
            //             shade: 0.2,
            //             maxmin: true,
            //             shadeClose: true,
            //             area: ['100%', '100%'],
            //             content: '../page/news/editor.html',
            //         });
            //         $(window).on("resize", function () {
            //             layer.full(index);
            //         });
            //     } else if (obj.event === 'delete') { // 监听删除操作
            //         var checkStatus = table.checkStatus('currentTableId')
            //         data = checkStatus.data;
            //         if (data.length > 0) {
            //             // 给id数值格式化为适合查询的格式
            //             var str = '';
            //             $.each(data, function (i, val) {
            //                 var id = val.id
            //                 if (i != 0) {
            //                     id = "," + id;
            //                 }
            //                 str += id;
            //             });
            //             var id = {
            //                 "id": str
            //             };
            //             id = JSON.stringify(id);
            //             layer.confirm('您确定要删除吗？', function (index) {
            //                 $.ajax({
            //                     url: '/news',
            //                     method: "DELETE",
            //                     data: id,
            //                     contentType: "application/json",
            //                     async: false,
            //                     error: function (err) {
            //                         layer.msg(err.msg, {
            //                             icon: 2
            //                         });
            //                     }
            //                 });
            //                 tableIns.reload();
            //                 layer.close(index);
            //             });
            //         } else {
            //             layer.msg("请至少选中一项！", {
            //                 icon: 2
            //             });
            //         }

            //     }
            // });
            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                var id = {
                    "id": data.id
                };
                if (obj.event === 'delete') {

                    id = JSON.stringify(id);
                    layer.confirm('您确定要删除吗？', function (index) {
                        $.ajax({
                            url: '/news',
                            method: "DELETE",
                            data: id,
                            contentType: "application/json",
                            error: function (err) {
                                layer.msg(err.msg, {
                                    icon: 2
                                });
                            }
                        })
                        obj.del();
                        layer.close(index);
                    });
                } else if (obj.event === 'edit') {
                    // 设置当前新闻id
                    $.cookie("news-id", data.id);
                    $.cookie("href","/page/news-approved-failed-admin.html")
                    var index = layer.open({
                        title: '编辑新闻',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '../page/news/edit-staff.html',
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                } else if (obj.event === 'read') { //预览新闻
                    $.cookie("news-id", data.id);
                    $.ajax({
                        url: '/news/edit',
                        method: "GET",
                        data: id,
                        contentType: "application/json",
                        success: function (data) {
                            data = data.data[0];
                            layer.open({
                                type: 1,
                                title: false,
                                closeBtn: 0,
                                resize: false,
                                shadeClose: true, //点击阴影关闭
                                area: ['375px', '500px'], //宽高
                                // offset: '20%',
                                content: '<div class="layuimini-container"><div class="layuimini-main"><div style="text-align: center;"><h1>' +
                                    data.title +
                                    '</h1></div><br><div style="text-align: center;">发布日期：' +
                                    data.date + '&nbsp;&nbsp;&nbsp;&nbsp;作者：<b>' +
                                    data.author +
                                    '</b></div><br><hr class="layui-border-black"><div>' +
                                    data.content + '</div></div></div>'
                            });
                        }
                    });

                }
            });

        });
    </script>
</body>

</html>